package com.quan.common.base.bean;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

/**
 * 分页工具类
 */
public class PageBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private long total = 0L;           //总记录数
    private int page = 1;           //当前页数
    private int limit = 10;         //每页记录数
    private long totalPage = 1;        //总页数
    private String sortName;           //排序名称
    private String sortOrder;          //排序类型（DESC：降序，ASC：降序）
    private String orderBy;            //排序字符串：ID DESC
    private String queryString;        //当前页所有参数

    public static void main(String[] args) {
        PageBean p = new PageBean() ;
        System.out.println("总记录数："+p.getTotal());
        System.out.println("当前页："+p.getPage());
        System.out.println("每页记录数："+p.getLimit());
        System.out.println("总页数："+p.getTotalPage());
    }

    public PageBean() {
    }

    /**
     * 如果请求为POST并且设置为@RequestBody将无法获取Paramster参数
     * @param request
     */
    public PageBean(HttpServletRequest request) {
        init(request);
    }

    public PageBean(long total) {
        this.total = total;
    }

    public PageBean(long total, int page, int limit) {
        this.total = total;
        this.page = page;
        this.limit = limit;
    }

    public PageBean(long total, int page, int limit, String orderBy) {
        this.total = total;
        this.page = page;
        this.limit = limit;
        this.orderBy = orderBy;
    }

    public PageBean(long total, int page, int limit, String sortName, String sortOrder) {
        this.total = total;
        this.page = page;
        this.limit = limit;
        this.sortName = sortName;
        this.sortOrder = sortOrder;
    }
    
    public PageBean(int page, int limit) {
		super();
		this.page = page;
		this.limit = limit;
	}

	public PageBean(int page, int limit, String orderBy) {
		super();
		this.page = page;
		this.limit = limit;
		this.orderBy = orderBy;
	}

	public PageBean(String orderBy) {
		this.orderBy = orderBy;
	}

	private void init(HttpServletRequest request) {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        String sortName = request.getParameter("sortName");
        String sortOrder = request.getParameter("sortOrder");
        String orderBy = request.getParameter("orderBy");
        this.setQueryString(request.getQueryString());
        
        if(!StringUtils.isEmpty(page)) {
            setPage(Integer.parseInt(page));
        }
        if(!StringUtils.isEmpty(limit)) {
            setLimit(Integer.parseInt(limit));
        }
        if(!StringUtils.isEmpty(sortName)) {
            setSortName(sortName);
        }
        if(!StringUtils.isEmpty(sortOrder)) {
            setSortOrder(sortOrder);
        }
        if(!StringUtils.isEmpty(orderBy)) {
            setOrderBy(orderBy);
        }
        if(!StringUtils.isEmpty(sortName) && !StringUtils.isEmpty(sortOrder)) {
            setOrderBy(this.sortName + " " + this.sortOrder);
        }
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }
    
    public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public long getTotalPage() {
        if(totalPage == 1) {
            return this.totalPage = (total % limit) == 0 ? (total / limit) : ((total / limit) + 1) ;
        } else {
            return this.totalPage;
        }
    }

    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

	@Override
	public String toString() {
		return "PageBean [total=" + total + ", page=" + page + ", limit=" + limit + ", totalPage="
				+ totalPage + ", sortName=" + sortName + ", sortOrder=" + sortOrder + ", orderBy=" + orderBy
				+ ", queryString=" + queryString + "]";
	}
    
}
